package com.hqu.environment.controller.admin;

import com.alibaba.fastjson.JSONObject;
import com.hqu.environment.annotation.LoginUser;
import com.hqu.environment.dao.EquipmentCategoryDao;
import com.hqu.environment.dto.EquipmentCategoryDTO;
import com.hqu.environment.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * @Description: 设备分类Controller
 * @Date: 2019/04/25 22:15
 * @Author: 邓勇嘉
 **/
@Controller
@RequestMapping("admin/equipmentCategory")
public class AdminEquipmentCategoryController {
    @Autowired
    EquipmentCategoryDao equipmentCategoryDao;


    @RequestMapping("/getList")
    @ResponseBody
    public Result getEquipmentCategoryList(EquipmentCategoryDTO equipmentCategoryDTO) {
        List<EquipmentCategoryDTO> equipmentCategoryList = equipmentCategoryDao.selectList(equipmentCategoryDTO);
        return Result.success().add("equipmentCategoryList",equipmentCategoryList);
    }

    @RequestMapping("/getById")
    @ResponseBody
    public Result getEquipmentCategoryById(Integer id) {
        EquipmentCategoryDTO equipmentCategoryDTO = equipmentCategoryDao.selectByPrimaryKey(id);
        return Result.success().add("equipmentCategory",equipmentCategoryDTO);
    }

    @RequestMapping("/deleteById")
    @ResponseBody
    public Result deleteById(Integer id) {
        equipmentCategoryDao.deleteByPrimaryKey(id);
        return Result.success();
    }


    @RequestMapping("/add")
    @ResponseBody
    public Result addEquipmentCategory(@LoginUser Integer userId, EquipmentCategoryDTO equipmentCategoryDTO) {
        equipmentCategoryDTO.setCreator(userId);
        equipmentCategoryDTO.setCreateTime(new Date());
        equipmentCategoryDao.insertSelective(equipmentCategoryDTO);
        return Result.success();
    }

    @RequestMapping("/update")
    @ResponseBody
    public Result updateEquipmentCategory(@LoginUser Integer userId, EquipmentCategoryDTO equipmentCategoryDTO) {
        equipmentCategoryDTO.setCreator(userId);
        equipmentCategoryDTO.setCreateTime(new Date());
        equipmentCategoryDao.updateByPrimaryKeySelective(equipmentCategoryDTO);
        return Result.success();
    }

    @RequestMapping("/deleteList")
    @ResponseBody
    public Result deleteEquipmentCategoryList(String ids) {
        List <Integer> list = JSONObject.parseArray(ids,Integer.class);
        equipmentCategoryDao.delete(list);
        return Result.success();
    }
}
